<?php
namespace app\api\validate;
use think\Db;
use think\Validate;

/**
 *
 * @group_name
 *
 * @author Master
 *
 * @api ${API}
 *
 * @time 2020/6/3 - 13:30
 *
 */
class UserLogin extends Validate{
    protected $rule = [
        'username' => 'require|login_check:username',
        'password' => 'require|min:5|max:16|login_check:password',
    ];

    protected $message = [
        'username.require' => '用户名不能为空',
        'password.require' => '密码不能为空',
        'password.min' => '密码长度最短为5位',
        'password.max' => '密码最长为十六位',
    ];
    //判断当前登录的用户是否存在，并通过md5解密
    public function login_check($value,$rule)
    {
        $username = input('username');
        switch ($rule) {
            case 'username':
                $result = Db::name("account")->where(["username" => $value, "data_state" => 1])->find();
                if ($result) {
                    return true;
                } else {
                    return "用户不存在";
                }
                break;
            case "password":
                $map = [
                    "username" => $username,
                    "password" => password_md5($value),
                    "data_state" => 1
                ];
                $checkPassword = Db::name("account")->where($map)->find();
                if ($checkPassword) {
                    return true;
                } else {
                    return '密码错误';
                }
                break;
            default:
                return "系统错误";
                break;
        }
    }
}